package com.example.score.controllrt;

import com.example.score.common.AjaxResult;
import com.example.score.entity.ClassCourse;
import com.example.score.entity.Course;
import com.example.score.entity.TUser;
import com.example.score.entity.Teaclass;
import com.example.score.service.ClassCourseService;
import com.example.score.service.ClassService;
import com.example.score.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private ClassCourseService classCourseService;
    @Autowired
    private ClassService classService;

    /**
     * 创建课程
     *
     * @param course
     * @return
     */
    @RequestMapping("/create")
    public AjaxResult create(Course course) {
        // 验证参数
        if (course == null) {
            return AjaxResult.fail(-1, "参数有误");
        }
        // 创建班级
        int i = courseService.create(course);
        if (i > 0) {
            return AjaxResult.success(course.getId());
        }
        return AjaxResult.fail(-2, "创建课程失败");
    }

    /**
     * 往课程导入班级
     *
     * @return
     */
    @Transactional
    @RequestMapping("/import")
    public AjaxResult imports(Integer courseId,Integer userId, @RequestParam("list")List<Integer> list, HttpServletRequest request) {
        // 验证参数
//        System.out.println(courseId);
//        System.out.println(list);
        if (courseId == null || courseId <= 0 || list == null || list.size() <= 0 || userId == null || userId <= 0 ) {
            return AjaxResult.fail(-1, "参数有误");
        }
        // 从session中获取当前登录对象
//        HttpSession session = request.getSession();
//        TUser user = (TUser) session.getAttribute("user");
//        int tid = user.getId();
        List<ClassCourse> clist = new ArrayList<>();
        for (Integer classId : list) {
            ClassCourse course = new ClassCourse();
            course.setCourse_id(courseId);
            course.setClass_id(classId);
            course.setCreateuse(userId);
            clist.add(course);
        }
        int insert = classCourseService.insert(clist);
        if (insert >= 0) {
            return AjaxResult.success("班级导入成功");
        }
        return AjaxResult.fail(-2, "班级导入失败");
    }
    @RequestMapping("/list")
    public AjaxResult getListByUserId(Integer userid) {
        if (userid == null || userid <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        List<Course> listByUserId = courseService.getListByUserId(userid);
        if (listByUserId != null && listByUserId.size() >= 0) {
            return AjaxResult.success(listByUserId);
        }
        return AjaxResult.fail(-2, "获取课程列表失败");
    }

    @RequestMapping("/members")
    public AjaxResult getMembersByCourseId(Integer courseId) {
        if (courseId == null || courseId <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        List<Teaclass> membersByCourseId = classService.getMembersByCourseId(courseId);
        if (membersByCourseId != null && membersByCourseId.size() >0) {
            return AjaxResult.success(membersByCourseId);
        }

        return AjaxResult.fail(-2, "获取班级列表失败");
    }

    @RequestMapping("/AllCourse")
    public AjaxResult getAllCourse(){
        List<Course> getAllCourse = courseService.getAllCourse();
        return AjaxResult.success(getAllCourse);
    }


//    课程管理

    @RequestMapping("/manage")
    public AjaxResult manage(Course course) {
        if (course == null || course.getId() <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        int manage = courseService.manage(course);
        if (manage > 0) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.fail(-2, "课程修改失败");
    }

    @RequestMapping("/showCourse")
    public AjaxResult manage(Integer CourseId) {
        if (CourseId == null || CourseId == 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        List<Course> showCourse = courseService.showCourse(CourseId);
        if (showCourse != null) {
            return AjaxResult.success(showCourse);
        }
        return AjaxResult.fail(-2, "课程修改失败");
    }

    @RequestMapping("/RemoveCourseClass")
    public AjaxResult manage(Integer CourseId,Integer ClassId) {
        System.out.println(CourseId);
        System.out.println(ClassId);
        if (CourseId == null || CourseId == 0 || ClassId==null || ClassId == 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        int showCourse = courseService.RemoveCourseClass(CourseId,ClassId);
        if (showCourse != 0) {
            return AjaxResult.success(showCourse);
        }
        return AjaxResult.fail(-2, "课程修改失败");
    }

    @RequestMapping("/deleteCourse")
    public AjaxResult deleteCourse(Integer CourseId) {
        if (CourseId == null || CourseId == 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        int showCourse = courseService.deleteCourse(CourseId);
        if (showCourse != 0) {
            return AjaxResult.success(showCourse);
        }
        return AjaxResult.fail(-2, "课程修改失败");
    }



}
